package com.sf.service.impl;

import com.sf.domain.Employee;
import com.sf.mapper.EmployeeMapper;
import com.sf.qo.EmployeeQueryObject;
import com.sf.service.IEmployeeService;
import com.sf.util.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

@Service
public class EmployeeServiceImpl implements IEmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;
    @Override
    public PageResult query(EmployeeQueryObject queryObject) {
        // 1 查询数据库总记录数
        /**
         * 这里为什么必须要传递queryObject
         * 因为在employee 当中存在着高级查询
         * 按照条件查询: 比如表中有10条数据 ,  按照条件查询是0条数据   问要分页吗?
         */
        Integer count = employeeMapper.selectForCount(queryObject);
        // 2 如果总记录数为0 不需要分页
        if(count == 0 ){
            return new PageResult(queryObject.getCurrentPage(),queryObject.getPageSize(),
                    Collections.emptyList(),0);
        }
        // 3 如果总为空0 就需要进行分页
        List<Employee> list = employeeMapper.selectForList(queryObject);
        return new PageResult(queryObject.getCurrentPage(),queryObject.getPageSize(),list,count);
    }

    @Override
    public void delete(Long id) {
        employeeMapper.delete(id);
    }

    @Override
    public Employee get(Long id) {
        return employeeMapper.get(id);
    }

    @Override
    public void saveOrUpdate(Employee employee) {
        // 判断是新增还是编辑  employe .getId 是否有值来进行区分
        if(employee.getId() == null){
            // 新增操作
            employeeMapper.insert(employee);
        }else{
            // 编辑操作
            employeeMapper.update(employee);
        }
    }
}
